package com.zhitan.ems.app.patrol.controller;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolTrackEntity;
import com.zhitan.ems.app.patrol.entity.PatrolTrackLogEntity;
import com.zhitan.ems.app.patrol.service.PatrolTrackLogService;
import com.zhitan.ems.app.patrol.service.PatrolTrackService;
import com.zhitan.ems.app.patrol.vo.PatrolTrackVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolTrackWrapper;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.utils.DateUtils;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 巡检轨迹
 * @author zhitan
 * 
 * @date 2024-11-02 14:05:23
 */
@Api(tags = "PC管理--巡检--巡检订单", value = "巡检订单")
@RestController
@RequestMapping("intra/patrol/patroltrack")
public class PatrolTrackController extends BaseController{
    @Autowired
    private PatrolTrackService patrolTrackService;
    @Autowired
    private PatrolTrackLogService patrolTrackLogService;
    /**
     * 列表
     */
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('patrol:patroltrack:list')")
    public R<VPage<PatrolTrackVO>> list(@RequestParam Map<String, Object> params){
        QueryWrapper<PatrolTrackEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolTrackEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
         if(params.containsKey("startDate")&&params.containsKey("endDate")) {
             queryWrapper.between("create_at",
                     DateUtils.getStartOfDate(DateUtils.strToDate(params.get("startDate").toString())),
                     DateUtils.getEndOfDate(DateUtils.strToDate(params.get("endDate").toString()))
               );   
         }
        IPage<PatrolTrackEntity> pages = patrolTrackService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(PatrolTrackWrapper.build().toVPageVO(pages));
    }
    @GetMapping("/listStat")
    @PreAuthorize("@ss.hasPermi('patrol:patroltrack:list')")
    public R<Map<String, Object>> listStat(@RequestParam Map<String, Object> params)throws ParseException{
         QueryWrapper<PatrolTrackEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolTrackEntity.class);
         queryWrapper.select("count(id) as total,sum(id) as all");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        if(params.containsKey("createDate")) {
            queryWrapper.between("create_at",
                    DateUtils.getStartOfDate(DateUtils.str_ToDate(params.get("createDate").toString())),
                    DateUtils.getEndOfDate(DateUtils.str_ToDate(params.get("createDate").toString()))
              );   
        }
        Map<String, Object>  stat=patrolTrackService.getMap(queryWrapper);
        return R.data(stat);
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = PatrolTrackVO.class)
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('patrol:patroltrack:info')")
    public R<PatrolTrackVO> detail(@PathVariable("id") Long id){
		PatrolTrackEntity patrolTrack = patrolTrackService.getById(id);
		PatrolTrackVO patrolTrackVO=PatrolTrackWrapper.build().entityVO(patrolTrack);
		QueryWrapper<PatrolTrackLogEntity> queryWrapper=new QueryWrapper<PatrolTrackLogEntity>();
		queryWrapper.eq(VQueryKit.getUnderColumn("patrolTrackId"), id);
		queryWrapper.orderByAsc("id");
		patrolTrackVO.setPatrolTrackLogList(patrolTrackLogService.list(queryWrapper));
	    return R.data(patrolTrackVO);
    }
     /**
     * 保存&更新
     */
    @ApiOperation(value = "保存&更新", response = Boolean.class)
    @PostMapping("/submit")
    @PreAuthorize("@ss.hasPermi('patrol:patroltrack:save')")
    public R<PatrolTrackVO> submit(@Valid @RequestBody PatrolTrackVO patrolTrack){
   		if(patrolTrack.getId()==null) {
            patrolTrack.setCreateAt(new Date());
            patrolTrack.setIsDelete(0);
            patrolTrack.setUpdateAt(new Date());
        } 
    	if(patrolTrackService.saveOrUpdate(patrolTrack)) {
    		 return R.ok(patrolTrack);
    	 }
    	return R.error("数据错误");
    }
    /**
     * 删除
     */
    @ApiOperation(value = "删除", response = Boolean.class)
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermi('patrol:patroltrack:delete')")
    public R<Boolean> delete(@RequestBody Long[] ids){
        return R.ok(patrolTrackService.removeByIds(Arrays.asList(ids)));
    }

}
